package com.cloud.security.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.cloud.common.domain.Result;
import com.cloud.domain.LoginUser;
import com.cloud.entity.SysUser;
import com.cloud.exception.CustomException;
import com.cloud.security.service.IProfileUserService;
import com.cloud.user_info.entity.vo.ProfileDept;
import com.cloud.user_info.entity.vo.ProfileUser;
import com.cloud.user_info.mapper.SysDeptMapper;
import com.cloud.user_info.mapper.SysUserMapper;
import com.cloud.utils.SecurityUtils;
import com.cloud.utils.StringUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 获取用户相关信息
 *
 * @author yzj
 */
@Service
public class ProfileUserServiceImpl implements IProfileUserService {

    @Resource
    private SysUserMapper userMapper;

    @Resource
    private SysDeptMapper deptMapper;

    @Override
    public Result getAuthUserInfo() {
        Map<String, Object> map = new HashMap<>();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNull(loginUser)) {
            return Result.error("查询失败:获取用户信息失败");
        }
        SysUser userInfo = loginUser.getUser();
        if (StringUtils.isNotNull(userInfo)) {
            ProfileUser proUser = userMapper.getProfileUserId(userInfo.getUserId());
            if (StringUtils.isNotNull(proUser)) {
                map.put("user", proUser);
                ProfileDept proDept = deptMapper.getProfileDeptId(proUser.getDeptId());
                map.put("dept", proDept);
                map.put("openId", getSysOpenId(loginUser.getSysCode(), userInfo.getUserId()));
            }
        }
        return Result.success("查询成功", map);
    }

    /**
     * 获取简介用户列表
     */
    @Override
    public List<ProfileUser> getUserList(ProfileUser entity) {
        return userMapper.getProfileUserList(entity);
    }

    /**
     * 获取简介用户信息
     */
    @Override
    public Result getUserInfo(ProfileUser entity) {
        if (StringUtils.isNotEmpty(entity.getUserName())
                && StringUtils.isEmpty(entity.getNickName())
                && StringUtils.isEmpty(entity.getPhone())
                && StringUtils.isEmpty(entity.getDutyPhone())) {
            throw new CustomException("查询失败:检索条件不能为空");
        }
        return Result.success("查询成功", userMapper.getProfileUserInfo(entity));
    }

    /**
     * 获取简介部门列表
     */
    @Override
    public List<ProfileDept> getDeptList(ProfileDept entity) {
        return deptMapper.getProfileDeptList(entity);
    }

    /**
     * 获取简介部门信息
     */
    @Override
    public Result getDeptInfo(ProfileDept entity) {
        if (StringUtils.isNull(entity.getDeptId())
                && StringUtils.isEmpty(entity.getDeptName())) {
            throw new CustomException("查询失败:检索条件不能为空");
        }
        return Result.success("查询成功", deptMapper.getProfileDeptInfo(entity));
    }

    /**
     * 获取系统应用OpenId
     */
    private String getSysOpenId(String sysCode, Long userId) {
        // 加密生成系统应用唯一用户标识
        return SecureUtil.md5(sysCode + "/" + userId);
    }

}
